package com.frame.auth.security.config;

import com.frame.auth.constant.SecurityConstants;
import com.frame.auth.mvc.dao.pojo.CloudSysUser;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回资源服务器数据配置
 * 很重要,资源服务器获取用户信息可在这里配置
 *
 * @auther zhangwj
 * @date 2021/4/14 下午3:15
 */
@Configuration
public class TokenEnhancerConfig implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
                                     OAuth2Authentication authentication) {
        final Map<String, Object> additionalInfo = new HashMap<>(4);
        CloudSysUser cloudSysUser = (CloudSysUser) authentication.getUserAuthentication().getPrincipal();
        additionalInfo.put(SecurityConstants.DETAILS_USER_ID, cloudSysUser.getUserId());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}
